import axios, { type AxiosResponse, type AxiosInstance, type AxiosRequestConfig } from 'axios'


import { getFullURL } from '@/utils/http'

const instance = axios.create({
  // Web 侧可以通过 vite.config.js 中的 proxy 配置，指定代理
  // 小程序APP里需写完整路径，如 https://service-rbji0bev-1256505457.cd.apigw.tencentcs.com/release
  baseURL: import.meta.env.VITE_BASE_API_URL,
  timeout: 60000,
  // ...config
})

instance.defaults.adapter = function(config) {
  console.log('request adapter ↓↓')
    const { url, method, data, params, headers, baseURL, paramsSerializer } = config
    return new Promise((resolve, reject) => {
      uni.request({
        method: method!.toUpperCase() as any,
        url: getFullURL(baseURL || '', url!, params, paramsSerializer),
        header: headers,
        data,
        dataType: 'json',
        responseType: config.responseType,
        success: (res: any) => {
          console.log('request success ↓↓')
          console.log(res)
          resolve(res)
        },
        fail: (err: any) => {
          reject(err)
        }
      })
    })
}

/**
     * 请求拦截
     */
instance.interceptors.request.use((config) => {
  console.log('config66')
  const { method, params } = config
  // 附带鉴权的token
  const headers: any = {
    Authorization: 'Bearer ' + uni.getStorageSync('token')
  }
  // 不缓存get请求
  // if (method === 'get') {
  //   headers['Cache-Control'] = 'no-cache'
  // }
  // delete请求参数放入body中
  if (method === 'delete') {
    headers['Content-type'] = 'application/json;'
    Object.assign(config, {
      data: params,
      params: {}
    })
  }

  return {
    ...config,
    headers
  }
})

/**
 * 响应拦截
 */
instance.interceptors.response.use((v) => {
  if (v.data?.code === 401) {
    uni.removeStorageSync('token')
    // alert('即将跳转登录页。。。', '登录过期')
    // setTimeout(redirectHome, 1500)
    return v.data
  }

  // @ts-ignore
  if ((v.status || v.statusCode) === 200) {
    return v.data
  }
  // alert(v.statusText, '网络错误')
  return Promise.reject(v)
})

export default instance

// export default {
//   //* 定义请求方法
//   // request<T = any>(config: AxiosRequestConfig): Promise<T> {
//   //   return instance.request(config)
//   // },

//   get<T = any>(url: string, params?: any, config?: AxiosRequestConfig): Promise<T> {
//     return instance.get(url, { params, ...config })
//   },

//   post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
//     console.log(instance.post, 'instancepost')
//     return instance.post(url, data, config)
//   },

//   put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
//     return instance.put(url, data, config)
//   },

//   delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
//     return instance.delete(url, config)
//   },
// }
